﻿using UnityEngine;
using System.Collections.Generic;
using System.IO;

public class ZoneGenerator
{
    private ZonePresent _ZonePresent;

    private string _filePath;

    public ZoneGenerator()
    {
        _ZonePresent = new ZonePresent(this);

        _Groud = new nLayerData();
        _Groud._layer_cell_h = ZoneCfg.RhombusMin;
        _Groud._layer_cell_l = ZoneCfg.RhombusMin / ZoneCfg.RhombusFactor;
    }

    List<nUnitData> _Building = new List<nUnitData>();
    List<nUnitData> _Deco = new List<nUnitData>();
    List<nUnitData> _Wall = new List<nUnitData>();
    List<nUnitData> _Foundation = new List<nUnitData>();
    public nLayerData _Groud = new nLayerData();

    public bool SeriZoneMap(int _Width, int _Height)
    {
        _Groud._layer_type = nLayerType.Rectangle;
        _Groud._Width = _Width;
        _Groud._Height = _Height;
        _Groud._layer_cell_name = "G_";
        _Groud._layer_res_name = string.Empty;

        {
            nRectangleVBOData _data = new nRectangleVBOData(_Groud._Width,
                                                            _Groud._Width / ZoneCfg.RhombusFactor,
                                                            _Groud._Width,
                                                            _Groud._Height);

            GameObject o = new GameObject("o");

            RhombusGridComponent _rgc = o.AddComponent<RhombusGridComponent>();
            _rgc.FormatRectangle(_data);

            int len = _rgc._grids.Length;

            List<int> _number = new List<int>(len);
            for (int i = 0; i < len; ++i)
                _number.Add(0);

            _Groud._grids_block = _number.ToArray();

            GameObject.DestroyImmediate(o);
        }

        return true;
    }

    public void Generate(string path)
    {
        _Wall.Clear();
        _Building.Clear();
        _Deco.Clear();
        _Foundation.Clear();
        //_Groud = null;

        _filePath = path;

        //SpriteRenderer[] allObjs = GameObject.FindGameObjectsWithTag<SpriteRenderer>("Zone");
        SpriteRenderer[] allObjs = GameObject.FindObjectsOfType<SpriteRenderer>();

        for (int i = 0; i < allObjs.Length; ++i)
        {
            SpriteRenderer sr = allObjs[i];

            if (sr.gameObject.tag != "Zone")
                continue;

            ////if (sr.name.StartsWith("G_"))
            ////{
            ////    _Groud = _ZonePresent.ToGroud(sr);
            ////    continue;
            ////}
            //if (sr.name.StartsWith("B_"))
            //{
            //    _Building.Add(_ZonePresent.ToBuilding(sr));
            //    continue;
            //}
            //if (sr.name.StartsWith("F_"))
            //{
            //    _Foundation.Add(_ZonePresent.ToBuilding(sr));
            //    continue;
            //}
            //if (sr.name.StartsWith("W_"))
            //{
            //    _Wall.Add(_ZonePresent.ToBuilding(sr));
            //    continue;
            //}
            //if (sr.name.StartsWith("D_"))
            //{
            //    _Deco.Add(_ZonePresent.ToBuilding(sr));
            //    continue;
            //}
        }

        SaveToFile();
    }

    public void LoadFromFile(string path)
    {
        string content = File.ReadAllText(path);

        nZoneData data = JsonUtility.FromJson<nZoneData>(content);


        for (int i = 0; i < data.walls.Count; ++i)
        {
            nUnitData ud = (nUnitData)data.walls[i];

            UnitObject co = new UnitObject(ud, ZoneCfg.CameraWorldLayer);
            co._Object.tag = "Zone";
        }


        //for (int i = 0; i < data.buildings.Count; ++i)
        //{
        //    nUnitData ud = (nUnitData)data.buildings[i];

        //    UnitObject co = new UnitObject(ud, ZoneCfg.CameraWorldLayer);
        //    co._Object.tag = "Zone";
        //}

        for (int i = 0; i < data.decos.Count; ++i)
        {
            nUnitData ud = (nUnitData)data.decos[i];

            UnitObject co = new UnitObject(ud, ZoneCfg.CameraWorldLayer);
            co._Object.tag = "Zone";
        }

        {
            nLayerData ud = (nLayerData)data._groud;

            LayerObject co = new LayerObject(ud, ZoneCfg.CameraGroudLayer);
            co._Object.tag = "Zone";

        }

        //for (int i = 0; i < data.foundations.Count; ++i)
        //{
        //    nUnitData ud = (nUnitData)data.foundations[i];

        //    UnitObject co = new UnitObject(ud, ZoneCfg.CameraWorldLayer);
        //    co._Object.tag = "Zone";

        //}

    }

    public void Reset()
    {

    }

    private void SaveToFile()
    {
        nZoneData _nZoneData = new nZoneData();
        _nZoneData._groud = _Groud;
        //_nZoneData.foundations = _Foundation;
        //_nZoneData.buildings = _Building;
        _nZoneData.walls = _Wall;
        _nZoneData.decos = _Deco;

        string data = JsonUtility.ToJson(_nZoneData);

        var streamWriter = new StreamWriter(_filePath);
        streamWriter.Write(data);
        streamWriter.Close();
    }
}
